﻿using System.Collections.Generic;
using System.Modules.Constants;
using System.Modules.Contracts.DTO;
using System.Modules.SharePoint.Contracts.Enums;
using System.Text;
using System.Modules.SharePoint.Constants;
using Microsoft.SharePoint;

namespace System.Modules.SharePoint.Contracts.DTO
{
    /// <summary>
    /// SP Query Date Ranges Overlap
    /// </summary>
    public class SPQueryDateRangesOverlap : SPQueryCondition
    {
        #region Properties
        /// <summary>
        /// Fields
        /// </summary>
        public List<SPQueryFieldRef> Fields { get; set; }

        /// <summary>
        /// Value
        /// </summary>
        public SPQueryCamlDateValues Value { get; set; }
        #endregion

        #region Implicit Operators
        /// <summary>
        /// Implicit conversion operator to string
        /// </summary>
        /// <param name="value">The SP Query Value</param>
        /// <returns>A string representation of the object</returns>
        public static implicit operator string(SPQueryDateRangesOverlap value)
        {
            return value == null ? string.Empty : value.ToString();
        }
        #endregion

        #region Override
        /// <summary>
        /// Overrides To String
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            var valueDateRangesOverlap = new StringBuilder();
            foreach (var field in Fields)
            {
                valueDateRangesOverlap.Append(field);
            }
            var value = new SPQueryValue(SPFieldType.DateTime, XmlTemplates.END_NODE) { IncludeTimeValue = true };
            valueDateRangesOverlap.AppendFormat(value, Value);
            return string.Format(SPQueryTemplates.DATE_RANGES_OVERLAP, valueDateRangesOverlap);
        }
        #endregion
    }
}
